<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
    <el-dialog title="密码编辑" :visible.sync="updatedialogFormVisible">
        <el-form :model="adminInfo" status-icon :rules="rules" ref="adminInfo" label-width="100px" class="demo-adminInfo">
            <el-form-item label="请输入旧密码" prop="password">
                <el-input v-model="adminInfo.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="请输入新密码" prop="newpassword">
                <el-input type="password" v-model="adminInfo.newpassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="checkPassWord">
                <el-input type="password" v-model="adminInfo.checkPassWord" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('adminInfo')">提交</el-button>
                <el-button @click="resetForm('adminInfo')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
<script>
    var danghui = new Vue({
        el: "#app",
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入'));
                } else {
                    if (danghui.adminInfo.checkPassWord !== '') {
                        danghui.$refs.adminInfo.validateField('checkPassWord');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== danghui.adminInfo.newpassword) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                updatedialogFormVisible: true,
                adminInfo: {
                    password:'',
                    newpassword: '',
                    checkPassWord: '',
                },
                rules: {
                    password: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    newpassword: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPassWord: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                }
            };
        },
        methods:{
            submitForm(formName) {
                danghui.$refs[formName].validate((valid) => {
                    if (valid) {
                        $.post("/ssm/admin/updatePassword",danghui.adminInfo, function (backData) {
                            if (backData.code == 2){
                                danghui.$message.error(backData.msg);
                            }else {
                                danghui.updatedialogFormVisible = false;
                                danghui.$message.success("修改成功");
                            }
                        });
                    } else {
                        console.log('error submit!!');
                return false;
            }
            });
            },
            resetForm(formName) {
                danghui.$refs[formName].resetFields();
            }

        },
    });

</script>
</body>
</html>